package com.wn32.Listener;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //tomcat运行就执行了(浏览器请求前)
        System.out.println("context 被实例化了");
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
        System.out.println("spring IOC容器注册完毕");
        sce.getServletContext().setAttribute("factory",context);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //关闭tomcat执行
        System.out.println("context 被销毁");
        sce.getServletContext().removeAttribute("factory");
        System.out.println("工厂被销毁");
    }
}
